package com.example.studentmanagement.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class MycourseDao {

    public static SQLiteDatabase db= DBUtil.db;


//    public void addChoose(String studentId , String curriculumId){
//        String chooseId = studentId + curriculumId ;
//
//        ContentValues values = new ContentValues();
//        values.put("chooseId" , chooseId);
//        values.put("studentId" , studentId);
//        values.put("curriculumId" , curriculumId);
//
//        long newRowId = db.insert("choose" , null ,values);
//
//    }
    public long addChoose(String studentId , String curriculumId){
        String chooseId = studentId + curriculumId ;
        ContentValues contentValues = new ContentValues() ;
        contentValues.put("chooseId" , chooseId);
        contentValues.put("studentId" , studentId);
        contentValues.put("curriculumId" , curriculumId);
        long newRowId = db.insert("choose" ,null , contentValues) ;
        return newRowId ;

}
    public boolean isCourseChosen(String studentId, String curriculumId) {
        String[] columns = { "chooseId" };
        String selection = "studentId = ? AND curriculumId = ?";
        String[] selectionArgs = { studentId, curriculumId };
        Cursor cursor = db.query("choose", columns, selection, selectionArgs, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.close();
            return true; // 课程已经被选择
        } else {
            cursor.close();
            return false; // 课程未被选择
        }
    }


    public boolean deleteChoose(String studentId, String curriculumId) {

        // 构建删除条件
        String whereClause = "studentId = ? AND curriculumId = ?";
        String[] whereArgs = { studentId, curriculumId };

        // 执行删除操作
        int rowsDeleted = db.delete("choose", whereClause, whereArgs);

        // 返回删除的行数，如果大于0，则表示删除成功
        return rowsDeleted > 0;
    }






    public List<String> getChosenCurriculumIds(String studentId) {
        List<String> curriculumIds = new ArrayList<>();
        String[] columns = { "curriculumId" };
        String selection = "studentId = ?";
        String[] selectionArgs = { studentId };
        Cursor cursor = null;
        try {
            cursor = db.query("choose", columns, selection, selectionArgs, null, null, null);
            while (cursor.moveToNext()) {
                String curriculumId = cursor.getString(0); // 索引0对应curriculumId
                curriculumIds.add(curriculumId);
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
//            db.close();
        }
        return curriculumIds;
    }

}
